package AMS.model;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class DemTable{ 
	public void setBuy(String stuid, int count, String del){	
		Demonbuy deb = new Demonbuy();   
		
		deb.setBuyid(stuid); 
		deb.setBuytime(new Timestamp(System.currentTimeMillis()));
		deb.setCount(count);  
		deb.setDel(del); 
       
		Session session = HibernateUtil.getSessionFactory().openSession();           
        Transaction tx= session.beginTransaction();         
        session.save(deb);
        tx.commit();         
        session.close();                             
        HibernateUtil.shutdown();             
    }
	public boolean getBuy(String stuid){	
	    Session session = HibernateUtil.getSessionFactory().openSession(); 
	    Query query = session.createQuery("from Demonbuy as deb where deb.buyid = ? and deb.del = ?");
	 		
	    query.setString(0, stuid);
	    query.setString(1, "False");
	  	  
	    List result = query.list();	
	     
	    Iterator it = result.iterator();
	     
	    ArrayList al = new ArrayList();
	     
	    while(it.hasNext()){
	    	al.add(it.next());
	    }  
	    
	    if(al.size()>0)
	    	return true;
	    else
	    	return false;
    }
	public void delDem(String stuid, String del){
        Session session = HibernateUtil.getSessionFactory().openSession(); 
	    Transaction tx= session.beginTransaction();
			
	    Query query = session.createQuery("update Demonbuy set Del =? where BuyId = ?");
	    query.setString(0, del);
	    query.setString(1, stuid);
			
	    query.executeUpdate();
	    tx.commit();
	    session.close();                 
    }
	public String getBuytime(String stuid){	
	    Session session = HibernateUtil.getSessionFactory().openSession(); 
	    Query query = session.createQuery("from Demonbuy as deb where deb.buyid = ? and deb.del = ?");
	 		
	    query.setString(0, stuid);
	    query.setString(1, "False");
	  	  
	    Timestamp buytime = new Timestamp(System.currentTimeMillis());  
	  
	    List result = query.list();	
	    
	    Iterator it = result.iterator();
	     
	    ArrayList al = new ArrayList();
	     
	    while(it.hasNext()){
	       al.add(it.next());
	    }     

	    if (al.size()==0)
	    	return "null";
	    else{
	    
	      for(Object aResult:result){
		    Demonbuy d = (Demonbuy)aResult;
		    buytime = d.getBuytime();	  
	      }
	      session.close(); 
	      return buytime.toString();
	    }
    }
	public int getCount(String stuid){	
	    Session session = HibernateUtil.getSessionFactory().openSession(); 
	    Query query = session.createQuery("from Demonbuy as deb where deb.buyid = ?");
	 		
	    query.setString(0, stuid);
	  	  
	    int count = 0;  
	  
	    List result = query.list();	
	  
	    for(Object aResult:result){
		   Demonbuy d = (Demonbuy)aResult;
		   count = d.getCount();
	    }	  
	    session.close();   	        
        return count;
    }
	public void setCount(String stuid, int count){
        Session session = HibernateUtil.getSessionFactory().openSession(); 
	    Transaction tx= session.beginTransaction();
			
	    Query query = session.createQuery("update Demonbuy as deb set deb.count =? where deb.buyid = ?");
	    query.setInteger(0, count);
	    query.setString(1, stuid);
			
	    query.executeUpdate();
	    tx.commit();
	    session.close();                 
    }
	public void setSteal(String stealerid, String stealeeid){	
		Demonuse deu = new Demonuse();   
		
		deu.setStealerid(stealerid);   
		deu.setStealeeid(stealeeid);
		deu.setStealtime(new Timestamp(System.currentTimeMillis()));
       
		Session session = HibernateUtil.getSessionFactory().openSession();           
        Transaction tx= session.beginTransaction();         
        session.save(deu);
        tx.commit();         
        session.close();                             
        HibernateUtil.shutdown();             
    }
	public ArrayList<String> getSteal(String stealeeid){ 	      
	    Session session = HibernateUtil.getSessionFactory().openSession(); 
	    Query query = session.createQuery("select deu.stealerid from Demonuse as deu where deu.stealeeid = ?");
	
	    query.setString(0, stealeeid);
	  	  
	    List result = query.list();	
	     
	    Iterator it = result.iterator();
	     
	    ArrayList<String> al = new ArrayList<String>();
	     
	    while(it.hasNext()){
	    	al.add((String) it.next());
	    }        
        return al; 
   }
   public ArrayList<Timestamp> getStealtime(String stealerid){ 	    // by asun  
	    Session session = HibernateUtil.getSessionFactory().openSession(); 
	    //Query query = session.createQuery("select deu.stealtime from Demonuse as deu where deu.stealerid = ? order by deu.stealtime desc");
	    Query query = session.createQuery("select act.time from Activity as act where act.stufrom = ? and act.relation = ? order by act.time desc");
	    query.setString(0, stealerid);
	    query.setString(1, "Tony");
	  	  
	    List result = query.list();	
	     
	    Iterator it = result.iterator();
	     
	    ArrayList<Timestamp> time = new ArrayList<Timestamp>();
	     
	    while(it.hasNext()){
	    	time.add((Timestamp) it.next());
	    }        
        return time; 
   }
}